package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.LabelItemsService;
import com.junzhiit.nengji.cms.web.exception.LabelItemsIsExistException;
import com.junzhiit.nengji.cms.web.exception.LabelItemsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.LabelNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.LabelItemsAddVO;
import com.junzhiit.nengji.cms.web.vo.LabelItemsEditVO;
import com.junzhiit.nengji.cms.web.vo.LabelItemsListVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.GoodsLabelItemsDAO;
import com.junzhiit.nengji.dao.LabelDAO;
import com.junzhiit.nengji.dao.LabelItemsDAO;
import com.junzhiit.nengji.model.GoodsLabelItems;
import com.junzhiit.nengji.model.Label;
import com.junzhiit.nengji.model.LabelItems;

@Service
public class LabelItemsServiceImpl extends BaseServiceImpl implements LabelItemsService{
	
	@Autowired
	private LabelItemsDAO labelItemsDAO;
	
	@Autowired
	private LabelDAO labelDAO;
	
	@Autowired
	private GoodsLabelItemsDAO goodsLabelItemsDAO;
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		LabelItems data=labelItemsDAO.query(id);
		result.setData(data);
		return result;
	}
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,LabelItemsListVO labelItemsListVO){
		
		LabelItems labelItems=new LabelItems();
		BeanUtils.copyProperties(labelItemsListVO, labelItems);
		
		List<LabelItems> list = labelItemsDAO.findPageAll(labelItems, dtp.getStart(), dtp.getLength());
		int totalCount = labelItemsDAO.getTotalCount(labelItems);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO add(LabelItemsAddVO labelItemsAddVO){
		ResultVO result=new ResultVO();
		
		Label label=labelDAO.query(labelItemsAddVO.getLabelId());
		if(label==null){
			throw new LabelNotFoundException("属性参数未找到");
		}

		LabelItems labelItems=new LabelItems();
		BeanUtils.copyProperties(labelItemsAddVO, labelItems);
		
		int count=labelItemsDAO.getIsExist(labelItems);
		if(count > 0){
			throw new LabelItemsIsExistException("参数值已经存在");
		}
		
		LabelItems data=labelItemsDAO.insert(labelItems);
		result.setData(data);
		
		return result;
	}
	
	@Override
	public ResultVO edit(String id,LabelItemsEditVO labelItemsEditVO){
		ResultVO result=new ResultVO();
		
		LabelItems labelItems=labelItemsDAO.query(id);
		if(labelItems==null){
			throw new LabelItemsNotFoundException("属性值找不到");
		}
		
		BeanUtils.copyProperties(labelItemsEditVO, labelItems);
		labelItemsDAO.update(labelItems);
		return result;
	}
	
	@Override
	public ResultVO updateTodelete(String id){
		ResultVO result=new ResultVO();
		
		LabelItems labelItems=labelItemsDAO.query(id);
		if(labelItems==null){
			throw new LabelItemsNotFoundException("属性参数未找到");
		}
		labelItemsDAO.updateToDelete(id);
		
		GoodsLabelItems goodsLabelItems=new GoodsLabelItems();
		goodsLabelItems.setLabelItemsId(id);
		goodsLabelItemsDAO.deleteLabel(goodsLabelItems);
		
		return result;
	}
}
